//
//  HYBroadCastController.m
//  XPlayer
//
//  Created by 张水 on 2023/10/25.
//

#import "HYBroadCastController.h"
#import "HYClientSocketController.h"
#import "HYBroadCastViewModel.h"
#import <HYSocketModel.h>
#import <HYFTFFIleTransfer/HYTCPServerManager.h>

@interface HYBroadCastController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSArray *dataSource;

@property(nonatomic, strong) HYBroadCastViewModel *viewModel;

@end

@implementation HYBroadCastController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = AppBgThemeColor;
    self.navigationBarView.titleLabel.text = NSLocalizedString(@"搜索", nil);
    self.navigationBarView.navagationBarStyle = Left_button_Show;
    [self.navigationBarView.leftButton setImage:[UIImage imageNamed:@"nav_btn_close_n"] forState:UIControlStateNormal];
    [self configUI];
    [self loadData];
    [self bindSignal];
}

- (void)loadData
{
    @weakify(self);
    [self.viewModel loadData:^{
        @strongify(self);
        [self.tableView reloadData];
    }];
    
    [self.viewModel.updatedContentSignal subscribeNext:^(HYHomeFileItemViewModel *videoModel) {
        @strongify(self);
        if (self.transfer) {
            self.transfer(videoModel);
        }
    }];
}
- (void)leftButtonClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)configUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.navigationBarView.mas_bottom);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(self.view.mas_bottom);
        }
    }];
}
- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.jumpSubJect subscribeNext:^(HYSocketModel *model) {
        @strongify(self);
        HYClientSocketController *clientVC = [[HYClientSocketController alloc]init];
        clientVC.host = model.host;
        clientVC.name = model.name;
        clientVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:clientVC animated:YES completion:nil];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLIDUITableViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor  = AppContentBgThemeColor;
    cell.textLabel.textColor = APPTextThemeColor;
    cell.detailTextLabel.textColor = APPTextThemeColor;
    HYSocketModel *model = [self.viewModel cellForRowAtIndexPath:indexPath];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.host;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectRowAtIndexPath:indexPath];
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = AppBgThemeColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELLIDUITableViewCell"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

- (HYBroadCastViewModel *)viewModel
{
    if (_viewModel == nil) {
        _viewModel = [[HYBroadCastViewModel alloc]init];
    }
    return _viewModel;
}
@end
